package com.ruoyi.web.controller.blogs;

import com.ruoyi.blogs.domain.BsTag;
import com.ruoyi.blogs.service.IBsTagService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 标签Controller
 *
 * @author ruoyi
 * @date 2022-10-28
 */
@RestController
@RequestMapping("/blogs/tag")
public class BsTagController extends BaseController {
    @Autowired
    private IBsTagService bsTagService;

    /**
     * 查询标签列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(BsTag bsTag) {
        startPage();
        List<BsTag> list = bsTagService.selectBsTagList(bsTag);
        return getDataTable(list);
    }

    /**
     * 查询全部标签列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:list')")
    @GetMapping("/allList")
    public AjaxResult allList(BsTag bsTag)
    {
        bsTag.setVisible(0);
        List<BsTag> list = bsTagService.selectBsTagList(bsTag);
        return AjaxResult.success(list);
    }

    /**
     * 导出标签列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:export')")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsTag bsTag) {
        List<BsTag> list = bsTagService.selectBsTagList(bsTag);
        ExcelUtil<BsTag> util = new ExcelUtil<BsTag>(BsTag.class);
        util.exportExcel(response, list, "标签数据");
    }

    /**
     * 获取标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bsTagService.selectBsTagById(id));
    }

    /**
     * 新增标签
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:add')")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsTag bsTag) {
        return toAjax(bsTagService.insertBsTag(bsTag));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:edit')")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsTag bsTag) {
        return toAjax(bsTagService.updateBsTag(bsTag));
    }

    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('blogs:tag:remove')")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bsTagService.deleteBsTagByIds(ids));
    }
}
